package com.itHsuDS.stack;

import java.util.Scanner;

public class ArrayStackDemo {
    public static void main(String[] args) {
        ArrayStack stack=new ArrayStack(4);
        String key="";
        boolean loop=true;
        Scanner sc=new Scanner(System.in);
        while (loop){
            System.out.println("show:显示栈");
            System.out.println("exit:退出程序");
            System.out.println("push:添加数据到栈");
            System.out.println("pop:从栈取出数据");
            System.out.println("请输入你的选择：");
            key=sc.next();
            switch (key){
                case "show":
                    stack.list();
                    break;
                case "exit":
                    sc.close();
                    loop=false;
                    break;
                case "push":
                    System.out.println("请输入一个数：");
                    int value=sc.nextInt();
                    stack.push(value);
                    break;
                case "pop":
                    try {
                        int res=stack.pop();
                        System.out.println("出栈的数据是："+res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                default:
                    break;
            }
        }
        System.out.println("退出程序");
    }
}

class ArrayStack{
    private int maxSize;
    private int[] stack;//数组模拟栈，存放数据
    private int top=-1;//表示栈顶，初始化为-1

    public ArrayStack(int maxSize){
        this.maxSize=maxSize;
        stack=new int[maxSize];
    }

    public boolean isFull(){
        return top==maxSize-1;//数组下标从0开始
    }

    public boolean isEmpty(){
        return top==-1;
    }

    public void push(int value){
        if(isFull()){
            System.out.println("栈满");
            return;
        }
        stack[++top]=value;
    }

    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("栈空");
        }
        return stack[top--];
    }

    //遍历时，从栈顶开始显示数据
    public void list(){
        if(isEmpty()){
            System.out.println("栈空");
            return;
        }
        for (int i = top; i >= 0; i--) {
            System.out.println("stack["+i+"]="+stack[i]+" ");
        }
    }
}

class LinkedStack {
    private static class Node {
        private int data;
        private Node next;

        public Node(int data) {
            this.data = data;
        }
    }

    private Node top;

    public boolean isEmpty() {
        return top == null;
    }

    public void push(int value) {
        Node newNode = new Node(value);
        if (isEmpty()) {
            top = newNode;
        } else {
            newNode.next = top;
            top = newNode;
        }
    }

    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈空");
        }
        int value = top.data;
        top = top.next;
        return value;
    }

    public void list() {
        if (isEmpty()) {
            System.out.println("栈空");
            return;
        }
        Node current = top;
        while (current != null) {
            System.out.println(current.data + " ");
            current = current.next;
        }
    }
}